package com.offcn.servlet;

import com.alibaba.fastjson.JSON;
import com.offcn.pojo.Course;
import com.offcn.pojo.User;
import com.offcn.service.CourseService;
import com.offcn.service.UserService;
import com.offcn.util.PageUtil;
import com.offcn.util.ResultVo;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/index")
public class IndexServlet extends BaseServlet {
    private UserService userService = new UserService();
    private ResultVo vo = null;
    private CourseService courseService = new CourseService();

    /*验证手机号*/
    public void checkPhone(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获取请求中的手机号
        String phone = request.getParameter("phone");
        //调用后台查询方法
        User user = userService.checkPhone(phone);
        if (user == null) {
            vo = new ResultVo(200, "手机号还没有被注册", null);
        } else {
            vo = new ResultVo(500, "手机号已经注册", null);
        }
        response.getWriter().write(JSON.toJSONString(vo));
    }

    /*登录功能*/
    public void login(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String phone = request.getParameter("phone");
        String pwd = request.getParameter("pwd");
        User user = userService.loginByPhone(phone, pwd);
        if (user!=null){
            vo = new ResultVo(200,"登录验证成功",user);
        }else {
            vo = new ResultVo(500,"账号密码有误，或是手机号未注册",null);
        }
        response.getWriter().write(JSON.toJSONString(vo));
    }

    /*首页课程信息查询*/
    public void queryByCourseByType(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Integer type = Integer.valueOf(request.getParameter("type"));
        Integer limit = Integer.valueOf(request.getParameter("limit"));
        //调用查询方法
        List<Course> courseList = courseService.queryByCourseByType(type, limit);
        vo = new ResultVo(200, "查询成功", courseList);
        response.getWriter().write(JSON.toJSONString(vo));
    }

    /*全部课程分页查询*/
    public void queryAllByPage(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获取当前的页码
        String currentPage = request.getParameter("currentPage");
        //获取每页显示条数
        String pageSize = request.getParameter("pageSize");
        //获取课程名称参数
        String courseName = request.getParameter("courseName");
        //获取课程类型参数
        String courseType = request.getParameter("courseType");
        //调用业务组装PageUtil
        PageUtil<Course> pageUtil = courseService.queryAllByPage(currentPage, pageSize, courseName, courseType);
        //转json
        vo = new ResultVo(200,"查询全部课程成功",pageUtil);
        response.getWriter().write(JSON.toJSONString(vo));
    }

    /*查询课程详情*/
    public void queryCourseById(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Integer cid = Integer.valueOf(request.getParameter("cid"));
        Course course = courseService.queryCourseById(cid);
        vo = new ResultVo(200,"查询成功",course);
        response.getWriter().write(JSON.toJSONString(vo));
    }
}
